package com.puboot.module.blog.controller;

import com.puboot.common.util.Constants;
import com.puboot.common.util.ElectricityGenerationUtil;
import com.puboot.common.util.GasDCUtil;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/eg/api")
@AllArgsConstructor
public class ElectricityGenerationController {

    private final HttpServletRequest httpServletRequest;
    private final ElectricityGenerationService electricityGenerationService;
    private final ElectricityGenerationNonCoalService electricityGenerationNonCoalService;
    private final ElectricityGenerationCoalService electricityGenerationCoalService;
    private final ElectricityGenerationDesulphurizationService electricityGenerationDesulphurizationService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(ElectricityGeneration electricityGeneration) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvis = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getNcvi())) {
                ncvis = Arrays.asList(electricityGeneration.getNcvi().split(","));
            }
            List<String> fcis = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getFci())) {
                fcis = Arrays.asList(electricityGeneration.getFci().split(","));
            }
            List<String> ccis = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getCci())) {
                ccis = Arrays.asList(electricityGeneration.getCci().split(","));
            }
            List<String> ofis = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getOfi())) {
                ofis = Arrays.asList(electricityGeneration.getOfi().split(","));
            }
            List<String> calks = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getCalk())) {
                calks = Arrays.asList(electricityGeneration.getCalk().split(","));
            }
            List<String> efks = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getEfk())) {
                efks = Arrays.asList(electricityGeneration.getEfk().split(","));
            }
            List<String> nvcs = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getNvc())) {
                nvcs = Arrays.asList(electricityGeneration.getNvc().split(","));
            }
            List<String> cs = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getC())) {
                cs = Arrays.asList(electricityGeneration.getC().split(","));
            }
            List<String> monthlyHeatins = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getMonthlyHeatin())) {
                monthlyHeatins = Arrays.asList(electricityGeneration.getMonthlyHeatin().split(","));
            }
            List<String> monthNames = new ArrayList<>();
            if (StringUtils.isNotBlank(electricityGeneration.getMonthName())) {
                monthNames = Arrays.asList(electricityGeneration.getMonthName().split(","));
            }
            List<String> ccs = ElectricityGenerationUtil.getSumcc(nvcs, cs);
            BigDecimal c = ElectricityGenerationUtil.getSumNvc(ccs, monthlyHeatins);
            BigDecimal ad = ElectricityGenerationUtil.getAd(c, electricityGeneration.getGResidue(),
                    electricityGeneration.getCResidue(), electricityGeneration.getGAsh(),
                    electricityGeneration.getCAsh(), electricityGeneration.getRemoveDust()
                    , monthlyHeatins, nvcs, cs);

            BigDecimal sumMonthlyHeatIn = ElectricityGenerationUtil.getSumMonthlyHeatin(monthlyHeatins);

            BigDecimal eburn = ElectricityGenerationUtil.getEBurn(ncvis, fcis, ccis, ofis);
            BigDecimal eDesulphurization = ElectricityGenerationUtil.getActivityDataFossilFuels(calks, efks);
            BigDecimal eElectric = ElectricityGenerationUtil.getEElectric(String.valueOf(electricityGeneration.getAdElectric()), String.valueOf(electricityGeneration.getEfElectric()));
            Double result = ElectricityGenerationUtil.getETotal(eburn.doubleValue(), eDesulphurization.doubleValue(), eElectric.doubleValue(), ad.doubleValue());
            electricityGeneration.setEBurn(GasDCUtil.round(eburn.doubleValue(), 2));
            electricityGeneration.setEDesulphurization(GasDCUtil.round(eDesulphurization.doubleValue(), 2));
            electricityGeneration.setResult(GasDCUtil.getThousandth(result));
            electricityGeneration.setCreateTime(new Date());
            electricityGeneration.setSumMonthlyHeatIn(sumMonthlyHeatIn.doubleValue());
            electricityGeneration.setCustomerId(customer.getId());
            electricityGeneration.setState(Constants.STATE_INVAILD);
            electricityGenerationService.save(electricityGeneration);

            for (int i = 0; i < nvcs.size(); i++) {
                ElectricityGenerationCoal coal = new ElectricityGenerationCoal();
                coal.setElectricityGenerationId(electricityGeneration.getId());
                coal.setNvc(Double.valueOf(nvcs.get(i)));
                if (cs.size() > i) {
                    coal.setC(Double.valueOf(cs.get(i)));
                }
                if (monthlyHeatins.size() > i) {
                    coal.setMonthlyHeatin(Double.valueOf(monthlyHeatins.get(i)));
                }
                if (monthNames.size() > i) {
                    coal.setMonthName(monthNames.get(i));
                }
                coal.setSortNumber(i);
                electricityGenerationCoalService.save(coal);
            }

            for (int i = 0; i < ncvis.size(); i++) {
                ElectricityGenerationNonCoal nonCoal = new ElectricityGenerationNonCoal();
                nonCoal.setElectricityGenerationId(electricityGeneration.getId());
                nonCoal.setNcvi(Double.valueOf(ncvis.get(i)));
                if (fcis.size() > i) {
                    nonCoal.setFci(Double.valueOf(fcis.get(i)));
                }
                if (ccis.size() > i) {
                    nonCoal.setCci(Double.valueOf(ccis.get(i)));
                }
                if (ofis.size() > i) {
                    nonCoal.setOfi(Double.valueOf(ofis.get(i)));
                }
                electricityGenerationNonCoalService.save(nonCoal);
            }

            for (int i = 0; i < calks.size(); i++) {
                ElectricityGenerationDesulphurization desulphurization = new ElectricityGenerationDesulphurization();
                desulphurization.setElectricityGenerationId(electricityGeneration.getId());
                desulphurization.setCalk(Double.valueOf(calks.get(i)));
                if (efks.size() > i) {
                    desulphurization.setEfk(Double.valueOf(efks.get(i)));
                }
                electricityGenerationDesulphurizationService.save(desulphurization);
            }
            return ResultUtil.success("计算成功", electricityGeneration);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
